#!/bin/bash

# Remote Diagnostic Script for YOLOv8 OpenVINO Project
# Usage: ./remote-diagnose.sh <host> <user> <password>

set -e

REMOTE_HOST="${1:-192.168.10.11}"
REMOTE_USER="${2:-asr}"
REMOTE_PASSWORD="${3:-123456}"
REMOTE_PATH="/home/asr/git/goyolopenvino"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[!]${NC} $1"; }

# Check sshpass
if ! command -v sshpass &> /dev/null; then
    log_error "sshpass not installed. Install with: brew install sshpass"
    exit 1
fi

# SSH wrapper
ssh_exec() {
    sshpass -p "$REMOTE_PASSWORD" ssh -o StrictHostKeyChecking=no \
        "${REMOTE_USER}@${REMOTE_HOST}" "$1"
}

log_info "=== Remote Environment Diagnostic ==="
log_info "Host: ${REMOTE_USER}@${REMOTE_HOST}"
log_info "Path: ${REMOTE_PATH}"
echo ""

# Test connection
log_info "Testing SSH connection..."
if ssh_exec "echo 'OK'" > /dev/null 2>&1; then
    log_success "SSH connection successful"
else
    log_error "SSH connection failed"
    exit 1
fi
echo ""

# System information
log_info "=== System Information ==="
ssh_exec "uname -a"
echo ""

# Check required tools
log_info "=== Required Tools ==="

tools=("go" "cmake" "g++" "make" "git")
for tool in "${tools[@]}"; do
    if ssh_exec "which $tool" > /dev/null 2>&1; then
        version=$(ssh_exec "$tool --version 2>&1 | head -1")
        log_success "$tool: $version"
    else
        log_error "$tool: NOT FOUND"
    fi
done
echo ""

# Check libraries
log_info "=== Required Libraries ==="

log_info "Checking OpenCV..."
if ssh_exec "pkg-config --modversion opencv4" > /dev/null 2>&1; then
    version=$(ssh_exec "pkg-config --modversion opencv4")
    log_success "OpenCV 4: $version"
elif ssh_exec "pkg-config --modversion opencv" > /dev/null 2>&1; then
    version=$(ssh_exec "pkg-config --modversion opencv")
    log_success "OpenCV: $version"
else
    log_error "OpenCV: NOT FOUND"
fi

log_info "Checking OpenVINO..."
if ssh_exec "test -d /opt/intel/openvino" > /dev/null 2>&1; then
    log_success "OpenVINO: Found at /opt/intel/openvino"
    ssh_exec "ls -la /opt/intel/openvino/runtime/lib/intel64/ 2>/dev/null | head -5"
else
    log_error "OpenVINO: NOT FOUND at /opt/intel/openvino"
    log_warning "Checking alternative locations..."
    ssh_exec "find /opt -name 'openvino' -type d 2>/dev/null || echo 'Not found'"
fi

log_info "Checking nlohmann_json..."
if ssh_exec "pkg-config --modversion nlohmann_json" > /dev/null 2>&1; then
    version=$(ssh_exec "pkg-config --modversion nlohmann_json")
    log_success "nlohmann_json: $version"
else
    log_warning "nlohmann_json: NOT FOUND (may be header-only)"
fi
echo ""

# Check project structure
log_info "=== Project Structure ==="
ssh_exec "ls -la ${REMOTE_PATH} | head -20"
echo ""

# Check build directories
log_info "=== Build Directories ==="
ssh_exec "test -d ${REMOTE_PATH}/YOLOv8-OpenVINO-CPP-Inference/build && \
    log_success 'C++ build dir exists' || log_warning 'C++ build dir not found'"
ssh_exec "test -d ${REMOTE_PATH}/goyolo/bin && \
    log_success 'Go bin dir exists' || log_warning 'Go bin dir not found'"
echo ""

# Check Go modules
log_info "=== Go Modules ==="
ssh_exec "cd ${REMOTE_PATH}/goyolo && go mod graph | head -10"
echo ""

# Check disk space
log_info "=== Disk Space ==="
ssh_exec "df -h ${REMOTE_PATH} | tail -1"
echo ""

# Check memory
log_info "=== Memory ==="
ssh_exec "free -h 2>/dev/null || vm_stat 2>/dev/null | head -5"
echo ""

log_success "Diagnostic complete!"

